import http from './interface'
import config from '../config.js'
import util from '@/common/util.js';
/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 * 
 */
export const setPath = (pname) => {
	var path = config[pname];
	var obj = {
		apiPost: (url, data) => {
			//设置请求前拦截器
			http.interceptor.request = (config) => {
				let url = config.url
				// 对部分接口需要将token放到请求头中
				let reg = /insertStuTime/g
				if (reg.test(url)) {
					config.header = {
						"token": uni.getStorageSync('token'),
						"reqMethod": 2,
						"Content-Type": "application/json"
					}
				}
				if(pname=="lessonUrl"){
					var header = url.substr(-13, 13);
					if (header == 'login/account') {
						config.header.Authorization=uni.getStorageSync('token')
					} else {
						config.header.Authorization=uni.getStorageSync('atoken')
					}
				}else if(pname=="serviceUrl"){
					config.header.Authorization=uni.getStorageSync('token')
				}else if(pname=='noteUrl'){
					let reg = /upLoadFile/g
					if (reg.test(url)) {
						config.header = {
							"Content-Type": "multipart/form-data"
						}
					}
				}
				else{
					delete config.header.Authorization
				}
			}
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				// console.log('个性化response....')
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url
			
			if(data&&pname!="lessonUrl") {
				if (data.hasOwnProperty('pwd')) {
					data.pwd = util.getDES(String(data.pwd))
				} else if (data.hasOwnProperty('user_id')) {
					data.user_id = util.getDES(String(data.user_id))
				} else if (data.hasOwnProperty('userId')) {
					data.userId = util.getDES(String(data.userId))
				} else if (data.hasOwnProperty('tc_user_id')) {
					data.tc_user_id = util.getDES(String(data.tc_user_id))
				} else if (data.hasOwnProperty('student_id')) {
					data.student_id = util.getDES(String(data.student_id));
				} else if (data.hasOwnProperty('mechanism_id')) {
					data.mechanism_id = util.getDES(String(data.mechanism_id))
				} else if (data.hasOwnProperty('uids')) {
					//uids 是 用户id 字符串集合，需要先拆分为数组后逐个加密再转为字符串集合
					var uids = data.uids.split(','); //用户ID集合数组
					var uids_ary = []; //新的数组存储加密后ID集合数组
					$.each(uids, function(i) {
						uids_ary.push(util.getDES(String(uids[i]))) //加密
					})
					data.uids = uids_ary.join(',') //字符串集合
				} else if (data.hasOwnProperty('admin_user_id')) {
					data.admin_user_id = util.getDES(String(data.admin_user_id));
				}
			}
		
			let token = {
				'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
			}
			data = Object.assign({},data,token)
			return http.request({
				url: dataurl,
				data
			})
		},
		apifile: (url, data) => {
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				// console.log('个性化response....')
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url

			let token = {
				'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
			}
			data = Object.assign({},data,token)
			return http.uploadfiles({
				url: dataurl,
				data
			})
		}
	}
	return obj;
}


// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {
	setPath
}
